package com.efast.dmom.aps.service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.dmom.aps.bean.ResultBean;
import com.efast.dmom.aps.bean.RouteOperation;
import com.efast.dmom.aps.datahandlers.RouteOperationDataHandler;
import com.efast.dmom.aps.datahandlers.RouteStepDataHandler;
import com.efast.dmom.aps.datahandlers.RouteStepWCRelDataHandler;
import com.efast.dmom.aps.service.RouteOperationService;
import com.efast.dmom.aps.util.StringVerifyUtil;

/**
 * @author Aaron Liu
 * @version Creation Time：2020年1月8日 下午4:23:23
 * @Data WJ modify 2020年2月18日
 */

@Service
public class RouteOperationServiceImpl implements RouteOperationService {

	@Autowired
	private RouteOperationDataHandler routeOperationDataHandler;
	
	@Autowired
	private RouteStepDataHandler RouteStepDataHandler;
	
	@Autowired
	private RouteStepWCRelDataHandler routeStepWCRelDataHandler;
	

	@Override
	public ResultBean save(RouteOperation routeOperation) {
		ResultBean resultBean = new ResultBean(true, ' ');
		if (!StringUtils.isNotBlank(routeOperation.getInnerData().getName())
				|| !StringUtils.isNotBlank(routeOperation.getInnerData().getOperationCode())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查工序名称、工序编号是否填写!");
			return resultBean;
		}
		RouteOperation routeOperationByCode = routeOperationDataHandler
				.getByCode(routeOperation.getInnerData().getOperationCode());
		if (routeOperationByCode.getInnerData() != null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("工序编号不能重复!");
			return resultBean;
		}
		RouteOperation routeOperationByName = routeOperationDataHandler
				.getByName(routeOperation.getInnerData().getName());
		if (routeOperationByName.getInnerData() != null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("工序名称不能重复!");
			return resultBean;
		}
		if (!StringVerifyUtil.isstring(routeOperation.getInnerData().getOperationCode())) {
		    resultBean.setSuccess(false);
		    resultBean.setMessage("工序编号不能由英文、字母、下划线组成!");
		    return resultBean;
		   }
		routeOperationDataHandler.save(routeOperation);
		resultBean.setSuccess(true);
		resultBean.setMessage("工序保存成功!");
		return resultBean;
	}

	@Override
	public ResultBean update(RouteOperation routeOperation) {
		ResultBean resultBean = new ResultBean(true, ' ');
		if (!StringUtils.isNotBlank(routeOperation.getInnerData().getName())
				|| !StringUtils.isNotBlank(routeOperation.getInnerData().getOperationCode())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查工序名称、工序编号是否填写!");
			return resultBean;
		}
		RouteOperation routeOperationById = routeOperationDataHandler
				.getById(routeOperation.getInnerData().getId());
		if (routeOperationById.getInnerData() == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("修改的工序不存在!");
			return resultBean;
		}
		if (!routeOperationById.getInnerData().getOperationCode().equals(routeOperation.getInnerData().getOperationCode())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("工序编码不可修改!");
			return resultBean;
		}
		if (!routeOperationById.getInnerData().getName().equals(routeOperation.getInnerData().getName())) {
			//Determine if the modified operation name is repeated
			RouteOperation routeOperationByName = routeOperationDataHandler
					.getByName(routeOperation.getInnerData().getName());
			if (routeOperationByName.getInnerData() != null) {
				resultBean.setSuccess(false);
				resultBean.setMessage("修改的工序名称不能重复!");
				return resultBean;
			}
		}
		routeOperationDataHandler.update(routeOperation);
		resultBean.setSuccess(true);
		resultBean.setMessage("工序修改成功!");
		return resultBean;
	}

	@Override
	public ResultBean delete(RouteOperation routeOperation) {
		ResultBean resultBean = new ResultBean(true, ' ');
		//verify that the operation is referenced by the route steps
		int qtyByRouteStep = RouteStepDataHandler.getRoueStepByOperationId(routeOperation.getInnerData().getId());
		if (qtyByRouteStep > 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("工序已被工艺步骤引用，无法删除!");
			return resultBean;
		}
		//verify that the operation is referenced by the route_wc_rel
		int qtyByRouteStepWcRel = routeStepWCRelDataHandler.getRouteStepWcRelByOperationId(routeOperation.getInnerData().getId());
		if (qtyByRouteStepWcRel > 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("工序已被工艺与工作中心关系表引用，无法删除!");
			return resultBean;
		}
		routeOperationDataHandler.delete(routeOperation);
		resultBean.setSuccess(true);
		resultBean.setMessage("工序删除成功!");
		return resultBean;
	}

	@Override
	public RouteOperation getById(long id) {
		return routeOperationDataHandler.getById(id);
	}

	@Override
	public RouteOperation getByName(String name) {
		return routeOperationDataHandler.getByName(name);
	}

	@Override
	public List<RouteOperation> getAllRouteOperation() {
		return routeOperationDataHandler.getAllRouteOperation();
	}

}
